package com.yuke.cloud.service.cmc.controller.frontend;

import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.cmc.entity.CmsSecKill;
import com.yuke.cloud.service.cmc.service.CmsSecKillService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* Created by wg on 2019/05/28.
*/
// todo 秒杀活动应该根据所在城市进行，每个城市的仓库仓库量不同
@Api(value = "WEB - CmsSecKillController", tags = "处理CmsSecKill相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/cms/sec_kill")
public class CmsSecKillController extends BaseController {
    @Resource
    private CmsSecKillService cmsSecKillService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "cmsSecKill", value = "该数据记录的POJO") @RequestBody CmsSecKill cmsSecKill) {
        cmsSecKill.setCreateUser(getLoginAuthDto().getUserId());
        cmsSecKill.setCreateTime(new Date());
        cmsSecKillService.save(cmsSecKill);
        return WrapMapper.ok(cmsSecKill.getSecKillId());
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        cmsSecKillService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "cmsSecKill", value = "需要修改的内容") @RequestBody CmsSecKill cmsSecKill) {
        cmsSecKill.setUpdateUser(getLoginAuthDto().getUserId());
        cmsSecKill.setUpdateTime(new Date());
        cmsSecKillService.update(cmsSecKill);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<CmsSecKill> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        CmsSecKill cmsSecKill = cmsSecKillService.findById(id);
        return WrapMapper.ok(cmsSecKill);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<CmsSecKill>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        PageHelper.orderBy("create_time desc");
        List<CmsSecKill> list = cmsSecKillService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    @DeleteMapping("/ids")
    @ApiOperation(httpMethod = "DELETE", value = "批量删除")
    public Wrapper deleteByIds(@ApiParam(name = "ids", value = "要删除的秒杀活动ID集") @RequestBody String secKillIds) {
        cmsSecKillService.deleteByIds(secKillIds);
        return WrapMapper.ok();
    }

    @PostMapping("/ifExist")
    @ApiOperation(httpMethod = "POST", value = "根据秒杀活动名称检查唯一性")
    public Wrapper ifExist(@ApiParam(name = "cmsSecKill", value = "记录secKillName") @RequestBody CmsSecKill cmsSecKill) {
        int count = cmsSecKillService.selectCount(cmsSecKill);
        if (cmsSecKill.getSecKillId() != null) {  // 排除自身
            count = 0;
        }
        return WrapMapper.ok(count > 0);
    }

    /**
     * 判断活动时间是否与其他活动的时间有重叠
     * @param cmsSecKill
     * @return
     */
    @PostMapping("/ifExistInTime")
    @ApiOperation(httpMethod = "POST", value = "判断活动时间是否与其他活动的时间有重叠")
    public Wrapper ifExistInTime(@ApiParam(name = "cmsSecKill", value = "记录活动时间") @RequestBody CmsSecKill cmsSecKill) {
        //把秒杀活动改为通用活动，短接此段代码
        if (2>1) {
            return WrapMapper.ok(0);
        }else {
            Example example = new Example(CmsSecKill.class);
            Example.Criteria criteria = example.createCriteria();
            if (cmsSecKill.getSecKillId() != null) {
                criteria.andNotEqualTo("secKillId", cmsSecKill.getSecKillId());
            }
            criteria.andEqualTo("isEnable", true);


            criteria.andCondition("((UNIX_TIMESTAMP( start_time ) BETWEEN " + cmsSecKill.getStartTime().getTime() / 1000 + " AND " + cmsSecKill.getEndTime().getTime() / 1000
                    + ") OR (UNIX_TIMESTAMP( end_time ) BETWEEN " + cmsSecKill.getStartTime().getTime() / 1000 + " AND " + cmsSecKill.getEndTime().getTime() / 1000 + "))");
            criteria.andCondition("sec_kill_type <", "3");
            int count = cmsSecKillService.selectCountByExample(example);

            return WrapMapper.ok(count > 0);
        }
    }
}
